package com.fullstuck.aop_annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 注解开发aop
 * 配置在 applicationContext-aop2.xml
 */
@Component("myAspect")
@Aspect   //代表当前类是一个切面类
public class MyAspect {

    //    配置切点表达式
    @Pointcut("execution(* com.fullstuck.aop_annotation.*.*(..))")
    public void pointcut() {}


    @Before("MyAspect.pointcut()")   //类.方法() 调用切点表达式
    public void before(){
        System.out.println("前置增强");
    }

    @AfterReturning("MyAspect.pointcut()")
    public void afterReturning(){
        System.out.println("后置增强 程序成功时才执行");

    }

    @Around("MyAspect.pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕前置增强");
        Object proceed = point.proceed();
        System.out.println("环绕后置增强");
        return proceed;

    }
    @AfterThrowing("MyAspect.pointcut()")
    public void afterThrowing(){
        System.out.println("异常抛出");

    }
    @After("MyAspect.pointcut()")
    public void after(){
        System.out.println("最终增强，不管是否报错都会执行");
    }
}
